package com.ufaud.ais.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.ufaud.ais.pub.exception.BusinessException;
import com.ufaud.ais.pub.service.impl.GenericCRUDServiceImpl;
import com.ufaud.ais.sys.dao.IDepartmentDao;
import com.ufaud.ais.sys.entity.Department;
import com.ufaud.ais.sys.entity.Employee;
import com.ufaud.ais.sys.service.IDepartmentService;
import com.ufaud.ais.sys.service.IEmployeeService;

public class DepartmentServiceImpl extends GenericCRUDServiceImpl<Department, Long> implements IDepartmentService {

	private IEmployeeService employeeService;

	public DepartmentServiceImpl(IDepartmentDao deptDao) {
		super(deptDao);
	}

	private IDepartmentDao getDao() {
		return (IDepartmentDao) iGenericCRUDDao;
	}

	public void deleteDepartmentById(Long id) throws BusinessException{
		Department persistedDept = getDao().get(id);
		Set<Department> subDeptSet = persistedDept.getSubDeptSet();
		Set<Employee> employeeSet = persistedDept.getEmployeeSet();
		if(subDeptSet != null && subDeptSet.size()>0) {
			throw new BusinessException("【"+persistedDept.getDeptName()+"】尚有子部门，无法删除");
		} else if (employeeSet != null && employeeSet.size()>0) {
			throw new BusinessException("【"+persistedDept.getDeptName()+"】尚有人员，无法删除");
		} else {
			 getDao().remove(id);
		}
	}

	public void updateDepartment(Department dept) {
		Long id = dept.getDeptId();
		Department persistedDept = getDao().get(id);
		persistedDept.setDeptName(dept.getDeptName());
		persistedDept.setDeptDesc(dept.getDeptDesc());
		persistedDept.setSuperDept(dept.getSuperDept());
		persistedDept.setDeptManager(dept.getDeptManager());
	}

	public Department getAllDepartmentInTreeStructure() {
		List<Department> departmentList = getDao().findByNamedQuery("getDeptTree", new HashMap<String, Object>());
		Department rootDept = new Department();
		rootDept.setDeptName("用友审计");
		for(Department dept : departmentList) {
			getDao().recInitSubDept(dept);
			getDao().evict(dept);
			rootDept.addSubDepartment(dept);
		}
		return rootDept;
	}
	
	public Department getWithSuperDeptAndDeptManager(Long id) {
		Department dept = get(id);
		getDao().initDeptManager(dept);
		getDao().initSuperDept(dept);
		return dept;
	}

	public List<Department> getDeptWithDescendantsById(Long id) {
		Department dept = getDao().get(id);
		List<Department> result = recLoadSubDeptToList(dept, new ArrayList<Department>());
		return result;
	}

	public List<Department> getAncestorAndSiblingDeptById(Long id) {
		List<Department> selfAndDescentantsList = getDeptWithDescendantsById(id);
		List<Department> resultList = getAll();
		resultList.removeAll(selfAndDescentantsList);
		return resultList;
	}

	private List<Department> recLoadSubDeptToList(Department dept, List<Department> list) {
		list.add(dept);
		Set<Department> subDeptSet = dept.getSubDeptSet();
		if (subDeptSet != null && subDeptSet.size() > 0) {
			for (Department oneSubDept : subDeptSet) {
				recLoadSubDeptToList(oneSubDept, list);
			}
		}
		return list;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

}
